package com.dco.dcowebhook.controller;

import com.dco.dcowebhook.dto.CommitStatusDTO;
import com.dco.dcowebhook.service.DataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

import java.util.List;

@Controller
public class HomeController {

        private final DataService dataService;

    @Autowired
    public HomeController(DataService dataService) {
        this.dataService = dataService;
    }

    /**
     * 处理主页的 GET 请求，并支持查询参数。
     *
     * @param model     传递给视图的模型
     * @param repoName  仓库名称（模糊查询）
     * @param repoOwner 仓库所有者（模糊查询）
     * @param pusher    推送者（模糊查询）
     * @return Thymeleaf 模板名称
     */
    @GetMapping({"/", "/index"})
    public String getHomePage(Model model,
                              String repoName,
                              String repoOwner,
                              String pusher) {
        List<CommitStatusDTO> commitStatuses = dataService.getCommitStatus(repoName, repoOwner, pusher);
        model.addAttribute("commitStatuses", commitStatuses);
        model.addAttribute("repoName", repoName);
        model.addAttribute("repoOwner", repoOwner);
        model.addAttribute("pusher", pusher);
        return "index";
    }
}
